<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>文章分类</title>
    <link rel="shortcut icon" href="/img/favicon.ico"/>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/common/layuimini/layui.css" media="all">
    <link rel="stylesheet" href="/common/layuimini/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px;">
                <div class="layui-form layui-form-pane">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">分类名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="categoryName" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">创建人</label>
                            <div class="layui-input-inline">
                                <input type="text" name="createBy" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button id="queryArticle" type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </div>
            </div>
        </fieldset>
        <table id="categoryTable" lay-filter="categoryTableFilter"></table>

        <script type="text/html" id="toolBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>
    </div>
</div>
</body>
<script type="text/html" id="currentTableBar" lay-filter="currentTableBar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
        <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除 </button>
<!--        <button class="layui-btn layui-btn-sm layui-btn-primary layui-border-green " lay-event="import"> 导入 </button>-->
    </div>
</script>
<script type="text/javascript" src="/common/layui/layui.js"></script>
<script type="text/javascript" src="/common/jquery-1.11.3.js"></script>
<script type="text/javascript" src="/common/xm-select/xm-select.js"></script>
<script type="text/javascript" src="/common/blog-ui.js"></script>
<script type="text/javascript">
    layui.use(['table','form','jquery'],function (){
        var table = layui.table;
        var form = layui.form;

        /**
         * 表格初始化
         * @param _
         * @return
         */
        table.render({
            elem: "#categoryTable",
            url: "/category/admin/tableList",
            toolbar: "#currentTableBar",
            id: "currentCategoryTable",
            cols: [
                [
                    {type: 'checkbox'},
                    {field: '', title: '序号',width:60,templet:function (d){
                            return d.LAY_INDEX;
                        }},
                    {field: 'categoryName', title: '分类',width:100},
                    {field: 'remark', title: '描述', width: 180},
                    {field: 'type', title: '类型', width: 100,templet(d){
                        if (d.type === 0){
                            return '系统内置';
                        }else {
                            return '用户添加';
                        }
                    }},
                    {field: 'enabled', title: '是否启用',event:'changeCategoryActive', width: 130,templet(d){
                        if (d.enabled === 1){
                            return '<input type="checkbox" value="'+d.id+'" checked="" name="open" lay-skin="switch" lay-filter="switchTest" lay-text="ON|OFF">'
                        }else {
                            return '<input type="checkbox" value="'+d.id+'" name="open" lay-skin="switch" lay-filter="switchTest" lay-text="ON|OFF">'
                        }
                        }},
                    {field: 'createBy', title: '创建人', width: 130},
                    {field: 'createTime', title: '创建时间', width: 180},
                    {field: 'updateTime', title: '修改时间', width: 180},
                    {fixed: 'right', title:'操作', toolbar: '#toolBar', width:150}
                ]
            ],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            page: true,
            skin: 'line',
            parseData: function(res){//将原始数据解析成 table 组件所规定的数据
                var result;
                if(this.page.curr){
                    result = res.data.slice(this.limit*(this.page.curr-1),this.limit*this.page.curr);
                }
                else{
                    result=res.data.slice(0,this.limit);
                }
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.count, //解析数据长度
                    "data": result //解析数据列表
                };
            }
        })

        //头工具栏事件
        table.on('toolbar(categoryTableFilter)', function(obj){
            if (obj.event === 'add') {  // 监听添加操作
                const url = "/category/admin/add";
                Pt.openModalBox("添加分类",url,"50%","50%");
            }else if (obj.event === 'delete'){
                Pt.showWarnMsg("delete")
            }
        });


        /**
         * 表格右侧操作按钮
         */
        table.on('tool(categoryTableFilter)',function (obj){
            console.log('-----------')
            console.log(obj);
            if (obj.event === 'edit'){
                const url = "/category/admin/edit?categoryId="+obj.data.id;
                Pt.openModalBox("编辑分类",url,"50%","50%");
            }
            if (obj.event === 'delete'){
                Pt.openConfirmBox("确定要删除吗",['确定','取消'],deleteCategory,obj.data.id)
            }
        })

        //表格状态开关
        form.on('switch(switchTest)', function(obj){
            var categoryId = this.value;
            console.log(this);
            var checked = this.checked;
            var sign = '';
            if (checked){
                sign = 'ON';
            }else {
                sign = 'OFF';
            }
            $.ajax({
                type: "post",
                url: "/category/admin/changeCategoryActive",
                data:{
                    "categoryId": categoryId,
                    "sign": sign
                },
                success:function (res){
                    if (res.state == true){
                        if (sign == 'ON'){
                            layer.msg('已启用',{icon:1,time:800})
                        }else {
                            layer.msg('已停用',{icon:1,time:800})
                        }
                    }else {
                        layer.msg(res.msg,{icon:2,time:800})
                        if (sign == 'ON'){
                            obj.elem.checked = false;
                        }else {
                            obj.elem.checked = true;
                        }
                        form.render();
                    }
                }
            })


        });

        form.on('submit(data-search-btn)', function (data) {
            console.log(data)
            table.reload('currentCategoryTable', {
                url: '/category/admin/tableList'
                , where: {
                    "createBy": data.field.createBy,
                    "categoryName": data.field.categoryName
                } //设定异步数据接口的额外参数
                //,height: 300
            });
        })

        /**
         * 删除分类
         * @param categoryId
         * @return
         */
        function deleteCategory(categoryIds){
            $.ajax({
                url: '/category/admin/deleteById',
                data:{
                    'categoryId' : categoryId
                },
                type: 'post',
                dataType: 'json',
                success: function (res){
                    if (res.state){
                        layui.table.reload('currentCategoryTable');
                        top.layer.closeAll();
                    }else {
                        Pt.showErrorMsg(res.msg)
                    }
                }
            })
        }

    })
</script>
</html>
